package com.wish.g04.financial.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g04.base.dto.JrProductDTO;
import com.wish.g04.base.entity.JrProduct;
import com.wish.g04.base.entity.Region;
import com.wish.g04.base.service.IJrProductService;
import com.wish.g04.base.service.IRegionService;
import com.wish.g04.common.core.web.controller.BaseController;
import com.wish.g04.common.core.web.domain.AjaxResult;
import com.wish.g04.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.g04.common.log.annotation.Log;
import com.wish.g04.common.log.enums.BusinessType;
import com.wish.g04.common.security.annotation.PreAuthorize;

/**
 * 金融产品管理Controller
 * 
 * @author Z-7
 * @date 2022-05-27
 */
@RestController
@RequestMapping("/product")
public class JrProductController extends BaseController
{
    @Autowired
    private IJrProductService jrProductService;
    @Autowired
    private IRegionService iRegionService;

    /**
     * 查询金融产品管理列表
     */
    @PreAuthorize(hasPermi = "financial:product:list")
    @GetMapping("/list")
    public TableDataInfo list(JrProductDTO jrProductDTO)
    {
        startPage();
        List<JrProduct> list = jrProductService.getProductList(jrProductDTO.getJrProductType());
        return getDataTable(list);
    }

    /**
     * 获取金融产品管理详细信息
     */
    @PreAuthorize(hasPermi = "financial:product:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(jrProductService.getById(id));
    }

    /**
     * 新增金融产品管理
     */
    @PreAuthorize(hasPermi = "financial:product:add")
    @Log(title = "金融产品管理-新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JrProductDTO jrProductDTO)
    {
        boolean result = jrProductService.edit(jrProductDTO);
        if (result) {
            return AjaxResult.success();
        }
        return AjaxResult.error("新增失败");
    }

    /**
     * 修改金融产品管理
     */
    @PreAuthorize(hasPermi = "financial:product:edit")
    @Log(title = "金融产品管理-修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JrProductDTO jrProductDTO)
    {
        boolean result = jrProductService.edit(jrProductDTO);
        if (result) {
            return AjaxResult.success();
        }
        return AjaxResult.error("编辑失败");
    }

    /**
     * 删除金融产品管理
     */
    @PreAuthorize(hasPermi = "financial:product:remove")
    @Log(title = "金融产品管理-作废", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        boolean result = jrProductService.removeLogicByIds(Arrays.asList(ids));
        if (result) {
            return AjaxResult.success();
        }
        return AjaxResult.error("操作失败");
    }

    /**
     * 获取所有的开户行所在的省
     * @return
     */
    @PreAuthorize(hasPermi = "financial:product:getProvince")
    @GetMapping("getAllBankAccountProvince")
    public AjaxResult getAllBankAccountProvince() {
        QueryWrapper<Region> wrapper = new QueryWrapper<>();
        wrapper.eq("PARENT_ID",1);
        List<Region> list = iRegionService.list(wrapper);
        return AjaxResult.success(list);
    }
}
